#include <atlbase.h>

#include <string>
#include <algorithm>
#include <memory>

#include "InputText.h"
#include "Miscellaneous.h"

using namespace std;


CInputText::CInputText(void)
{
}


CInputText::~CInputText(void)
{
}

void CInputText::DoFill(IHTMLDocument2* document) throw(...)
{
	if(!document)
		throw wstring(L"Input parameter document is null!");

	auto e = this->FindElement(document);

	this->SetText(e);
	this->RaiseEvent(e);
}

//void CInputText::InitElementFromXml(IXmlReader* pReader) throw(...)
//{
//	HRESULT hr;
//	XmlNodeType nodeType;
//	const WCHAR* pwszBuffer;
//
//	
//	//TODO:
//	CElement::InitElementFromXml(pReader);
//}

void CInputText::BuildAvailableAttributes() throw()
{
	_maa[L"type"]		= L"";
	_maa[L"value"]		= L"";
	_maa[L"trigger"]	= L"onchange";
}


void CInputText::ValidateAttributes() const throw(...)
{
	if(_maa.find(L"value")->second == L"")
		throw wstring(L"Attribute 'value' cannot be null or empty");
}

void CInputText::SetText(IHTMLElement* e) const throw(...)
{
	HRESULT hr;
	const wstring& value = _maa.find(L"value")->second;

	hr = e->setAttribute(CComBSTR(L"value"), CComVariant(value.c_str()));
	if(FAILED(hr))
		throw wstring(L"Cannot set value to element, tag->") + _tag;
}

void CInputText::RaiseEvent(IHTMLElement* e) const throw(...)
{
	CMiscellaneous::RaiseEvent(e, _maa.find(L"trigger")->second);
}